﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace OgilvyOne.BLL
{
   public class DataCache
    {
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];

        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string cacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;          
            if (objCache[cacheKey] != null)
            {
                objCache[cacheKey] = objObject;
            }
            else
            {
                objCache.Insert(cacheKey, objObject);
            }


        }

       /// <summary>
       /// 设置当前应用程序指定CacheKey的Cache值
       /// </summary>
       /// <param name="cacheKey"></param>
       /// <param name="objObject"></param>
       /// <param name="absoluteExpiration"> </param>
       /// <param name="slidingExpiration"> </param>
       public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            if (objCache[cacheKey] != null)
            {
                objCache[cacheKey] = objObject;
            }
            else
            {
                objCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
            }
        }

        /// <summary>
        /// 删除当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static void RemoveCache(string cacheKey)
        {
            if (cacheKey == null) throw new ArgumentNullException("cacheKey");
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(cacheKey);
            return;
        }
        
        
    }
}
